package com.zhuxulin.tab;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;

import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelUuid;

import com.zhuxulin.midi.util.MidiProcessor;
import com.zhuxulin.tab.bluetooth.ISendMsg;
import com.zhuxulin.tab.bluetooth.PlayMusic;
import com.zhuxulin.tab.bluetooth.SendMessage;
import com.zhuxulin.tab.utils.LogUtils;

public class App extends Application {

	private static String TAG = App.class.getSimpleName();
	private BluetoothAdapter adapter = null;
	private BluetoothDevice device = null;
	private BluetoothSocket socket = null;
	// 00001101-0000-1000-8000-00805F9B34FB
	//
	// 00001108-0000-1000-8000-00805F9B34FB
	//
	// 0000110B-0000-1000-8000-00805F9B34FB
	//
	// 0000110E-0000-1000-8000-00805F9B34FB
	//
	// 0000111E-0000-1000-8000-00805F9B34FB
	private UUID id = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
	private OutputStream outputStream = null;
	private InputStream inputStream = null;
	private ISendMsg sendMsg = null;
	private MidiProcessor processor = null;
	private PlayMusic playMusic = null;
	// private boolean isInit=false;
	// private boolean isFailed=false;

	public Handler socketConnectedHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				LogUtils.i(TAG, "Socket connect falid!");
				// isFailed=true;
				// isInit=false;
				break;
			case 1:
				LogUtils.i(TAG, "Socket connect successful!");
				try {
					LogUtils.i(TAG, "Begin getOutputSream!");
					if (socket.isConnected()) {
						outputStream = socket.getOutputStream();
						LogUtils.i(TAG, "Successful getOutputSream!");
						LogUtils.i(TAG,
								"outputStream:" + outputStream.toString());
					} else {
						LogUtils.i(TAG, "Socket is not connected!");
						LogUtils.i(TAG, "Failed to getOutputSream!");
					}
				} catch (IOException e) {
					LogUtils.i(TAG, "getOutputStream throws exception!");
					e.printStackTrace();
				}
				try {
					LogUtils.i(TAG, "Begin getInputStream!");
					if (socket.isConnected()) {
						inputStream = socket.getInputStream();
						LogUtils.i(TAG, "Successful getInputStream!");
						LogUtils.i(TAG, "inputStream:" + inputStream.toString());
					} else {
						LogUtils.i(TAG, "Socket is not connected!");
						LogUtils.i(TAG, "Failed to getInputStream!");
					}
				} catch (IOException e) {
					LogUtils.i(TAG, "getInputStream throws exception!");
					e.printStackTrace();
				}
				if (inputStream != null && outputStream != null) {
					sendMsg = new SendMessage(App.this);
					// isInit=true;
					// isFailed=false;
					// sendMsg.sendMessage(0x90);
					// sendMsg.sendMessage(0x3C);
					// sendMsg.sendMessage(0x7F);
				}
				break;
			}
		}
	};

	// private Button play = null;
	// private Button bluetooth = null;
	// private Button send = null;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		LogUtils.i(TAG, "App onCreate()");
		// adapter=BluetoothAdapter.getDefaultAdapter();
		// device=getBluetoothDevice();
		// socket=createBluetoothSocket(device);
		// new SocketConnect(this,socketConnectedHandler).start();
	}

	private BluetoothDevice getBluetoothDevice() {
		BluetoothDevice device = null;
		if (adapter == null) {
			LogUtils.i(TAG, "adapter is null can not get blue tooth device!");
			return null;
		}
		LogUtils.i(TAG, "Begin getBluetoothDevice!");
		device = adapter.getBondedDevices().iterator().next();
		LogUtils.i(TAG, "End getBluetoothDevice!Add:" + device.getAddress()
				+ "Name:" + device.getName() + "UUIDs:" + device.getUuids());
		for (ParcelUuid p : device.getUuids()) {
			LogUtils.i(TAG, "P:" + p.toString());
			LogUtils.i(TAG, "UUID:" + p.getUuid().toString());
		}
		return device;
	}

	private BluetoothSocket createBluetoothSocket(BluetoothDevice device) {
		if (device == null) {
			LogUtils.i(TAG, "Device is null can not create socket!");
			return null;
		}
		BluetoothSocket socket = null;
		try {
			LogUtils.i(TAG,
					"Use UUID to create socket at createBluetoothSocket(BluetoothDevice device)");
			// adapter.listenUsingInsecureRfcommWithServiceRecord(name, id);
			// return device.createRfcommSocketToServiceRecord(id);
			socket = device.createInsecureRfcommSocketToServiceRecord(id);
			LogUtils.i(TAG, "Use UUID to create socket successful!");
			LogUtils.i(TAG, "Socket:" + socket.toString());
			return socket;
		} catch (IOException e) {
			LogUtils.e(TAG,
					"Throws exception at createBluetoothSocket(BluetoothDevice device)");
			e.printStackTrace();
			return null;
		}
	}

	private BluetoothSocket createBluetoothSocket1(BluetoothDevice device) {
		Method m = null;
		try {
			m = device.getClass().getMethod("createRfcommSocket",
					new Class[] { int.class });
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		try {
			socket = (BluetoothSocket) m.invoke(device, Integer.valueOf(2));
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return socket;
	}

	public OutputStream getOutputStream() {
		return outputStream;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public BluetoothSocket getSocket() {
		return socket;
	}

	public ISendMsg getSendMsg() {
		return sendMsg;
	}

	// public boolean isInit() {
	// return isInit;
	// }
	//
	// public boolean isFailed() {
	// return isFailed;
	// }

	public void setSocket(BluetoothSocket socket) {
		this.socket = (socket == null ? socket : this.socket);
	}

	public void setOutputStream(OutputStream outputStream) {
		this.outputStream = outputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public void setSendMsg(ISendMsg sendMsg) {
		this.sendMsg = sendMsg;
	}

	public MidiProcessor getProcessor() {
		return processor;
	}

	public void setProcessor(MidiProcessor processor) {
		this.processor = processor;
	}

	public BluetoothAdapter getAdapter() {
		return adapter;
	}

	public void setAdapter(BluetoothAdapter adapter) {
		this.adapter = (adapter == null ? adapter : this.adapter);
	}

	public BluetoothDevice getDevice() {
		return device;
	}

	public void setDevice(BluetoothDevice device) {
		this.device = (device == null ? device : this.device);
	}

	public PlayMusic getPlayMusic() {
		return (playMusic == null) ? new PlayMusic(this) : playMusic;
	}

	public void setPlayMusic(PlayMusic playMusic) {
		this.playMusic = (playMusic == null ? new PlayMusic(this) : playMusic);
	}
}
